﻿<?php
session_start();

if(!isset($_SESSION['id'])){
	header('Location:login.php');
}
$action=$_GET['action'];
$type=trim($_GET['type']);
$move=intval($_GET['move']);

$db=new PDO("mysql:host=localhost;dbname=shenji","root","");
$db->setAttribute(PDO::ATTR_EMULATE_PREPARES,false);
$db->query("set names utf8");
$sql="select * from shenji where move=? and type=?";
$result=$db->prepare($sql);
$result->execute(array($move,$type));
$row=$result->fetch(PDO::FETCH_ASSOC);
$version=$row['version'];

if($action==up){  //版本上移
	//得到最大的
	$sql2="select max(move) from shenji where type='".$type."'";
	$result2=$db->prepare($sql2);
	$result2->execute();
	$row2=$result2->fetch(PDO::FETCH_ASSOC);	
	$maxmove=$row2['max(move)'];
	if($move<$maxmove){
		/*$smove=$move+1;
		$sql3="select * from shenji where move='".mysql_real_escape_string($smove)."' and type='".$type."'";
		$result3=$db->query($sql3);
		$result3->setFetchMode(PDO::FETCH_ASSOC);
		$row3=$result3->fetch();
		$version2=$row3['version'];
		$sql4="update shenji set move=$smove where version=$version and type='".$type."'";
		$result4=$db->exec($sql4);
		$sql5="update shenji set move=$move where version=$version2 and type='".$type."'";
		$result5=$db->exec($sql5);*/
		$smove=$move+1;
		$sql3="select * from shenji where move=? and type=?";
		$result3=$db->prepare($sql3);
		$exeres=$result3->execute(array($smove,$type));
		if($exeres){
			$row3=$result3->fetch(PDO::FETCH_ASSOC);
			$version2=$row3['version'];
		}
		$sql4="update shenji set move=? where version=? and type=?";
		$result4=$db->prepare($sql4);
		$result4->execute(array($smove,$version,$type));
		$sql5="update shenji set move=? where version=? and type=?";
		$result5=$db->prepare($sql5);
		$result5->execute(array($move,$version2,$type));
		
	}
}elseif($action==down){    //版本下移
	if($move>100){
		//得到下一个
		/*$xmove=$move-1;
		$sql3="select * from shenji where move='".mysql_real_escape_string($xmove)."' and type='".$type."'";
		$result3=$db->query($sql3);
		$result3->setFetchMode(PDO::FETCH_ASSOC);
		$row3=$result3->fetch();
		$version2=$row3['version'];
		$sql4="update shenji set move=$xmove where version=$version and type='".$type."'";
		$result4=$db->exec($sql4);
		$sql5="update shenji set move=$move where version=$version2 and type='".$type."'";
		$result5=$db->exec($sql5);*/
		$xmove=$move-1;
		$sql3="select * from shenji where move=? and type=?";
		$result3=$db->prepare($sql3);
		$exeres=$result3->execute(array($xmove,$type));
		if($exeres){
			$row3=$result3->fetch(PDO::FETCH_ASSOC);
			$version2=$row3['version'];
		}
		$sql4="update shenji set move=? where version=? and type=?";
		$result4=$db->prepare($sql4);
		$result4->execute(array($xmove,$version,$type));
		$sql5="update shenji set move=? where version=? and type=?";
		$result5=$db->prepare($sql5);
		$result5->execute(array($move,$version2,$type));
	}  
} 
$db=null;
echo "<script>location='select_shenji.php';</script>";

?>